﻿Imports Microsoft.Office.Interop
Public Class fVendedores
    Dim daVend As lbGlobalcom.dsTerceroTableAdapters.TER_VENDEDORTableAdapter
    Dim daTipoVend As lbGlobalcom.dsTerceroTableAdapters.TER_TIPOVENDEDORTableAdapter
    Dim daZona As lbGlobalcom.dsGlobalTableAdapters.GLO_ZONASTableAdapter
    Private Sub fVendedores_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        AddHandler Me.SalvaTerecero, AddressOf Salvar
        AddHandler Me.BuscaTercero, AddressOf BuscaVendedor
        AddHandler Me.EliminaTercero, AddressOf EliminaVendedor
        MyBase.MustraMsjSalva = False
        AddHandler Me.AddTercero, AddressOf AddVendedor
        AddHandler Me.ExporToExcel, AddressOf ExportVenToExcel
        daVend = New lbGlobalcom.dsTerceroTableAdapters.TER_VENDEDORTableAdapter
        daTipoVend = New lbGlobalcom.dsTerceroTableAdapters.TER_TIPOVENDEDORTableAdapter
        daZona = New lbGlobalcom.dsGlobalTableAdapters.GLO_ZONASTableAdapter
        TER_TERCEROBindingNavigator.Items("BindingNavigatorAddNewItem").Enabled = False
        TER_TERCEROBindingNavigator.Items("BindingNavigatorDeleteItem").Enabled = False
    End Sub
    Private Sub ExportVenToExcel()
        Try
            Me.Cursor = Cursors.WaitCursor
            Dim cex As New lbGlobalcom.cExcel(lbGlobalcom.DatosTerceros.GetVendedores())
            Dim m_Excel As Excel.Application
            m_Excel = cex.exportarExcel
            m_Excel.Visible = True
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            Me.Cursor = Cursors.Default
        End Try
    End Sub
    Private Sub SetEstadoVendedor(ByVal ValorEstado As Boolean)
        IDTIPOVENDEDORTextBox.Enabled = ValorEstado
        IDZONATextBox.Enabled = ValorEstado
        ACUMULACOMISIONListBox.Enabled = ValorEstado
        btnBuscaTipoVendedor.Enabled = ValorEstado
        btnBuscaZona.Enabled = ValorEstado
    End Sub
    Public Sub Salvar()
        Try
            TER_VENDEDORBindingSource.EndEdit()
            daVend.Update(DsTercero.TER_VENDEDOR)
            MessageBox.Show("Se salvo registro con exito", "Salvar Registro", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Public Sub BuscaVendedor(ByVal idTercero As String)
        Try
            daVend.FillByIdVendedor(DsTercero.TER_VENDEDOR, idTercero)
            If DsTercero.TER_VENDEDOR.Rows.Count = 0 Then
                SetEstadoVendedor(False)
            Else
                SetEstadoVendedor(True)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Public Sub EliminaVendedor()
        Try
            If MessageBox.Show("Esta seguro que desea eliminar el vendedor", "Elimina Vendedro", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                TER_VENDEDORBindingSource.Remove(TER_VENDEDORBindingSource.Current)
                daVend.Update(DsTercero.TER_VENDEDOR)
                SetEstadoVendedor(False)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Public Sub AddVendedor()
        Try
            Dim drVend As lbGlobalcom.dsTercero.TER_VENDEDORRow = DsTercero.TER_VENDEDOR.NewTER_VENDEDORRow
            drVend.IDVENDEDOR = IDTERCEROTextBox.Text
            DsTercero.TER_VENDEDOR.Rows.Add(drVend)
            drVend = Nothing
            SetEstadoVendedor(True)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub btnBuscaTipoVendedor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaTipoVendedor.Click
        Dim flTipoVen As New fListaTipoVendedor
        If flTipoVen.ShowDialog = Windows.Forms.DialogResult.OK Then
            IDTIPOVENDEDORTextBox.Text = flTipoVen.idTipoVend
        End If

    End Sub


    Private Sub IDTIPOVENDEDORTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IDTIPOVENDEDORTextBox.TextChanged
        If IDTIPOVENDEDORTextBox.Text.Trim.Length > 0 Then
            lbTipoVendedor.Text = daTipoVend.GetDescripcionTipoVend(IDTIPOVENDEDORTextBox.Text)
        Else
            lbTipoVendedor.Text = ""
        End If
    End Sub

    Private Sub btnBuscaZona_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaZona.Click
        Dim fLisZon As New fListaZonas
        If fLisZon.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.IDZONATextBox.Text = fLisZon.midZona
        End If
    End Sub

    Private Sub IDZONATextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IDZONATextBox.TextChanged
        If IDZONATextBox.Text.Trim.Length > 0 Then
            lbZona.Text = daZona.GetDescripcionZona(IDZONATextBox.Text)
        Else
            lbZona.Text = ""
        End If
    End Sub


    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        AddVendedor()
    End Sub

    Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
        EliminaVendedor()
    End Sub
End Class
